// pages/index/index.js
const Page = require('../../../utils/ald-stat.js').Page
const util = require('../../../utils/throttle.js')
// const MD5 = require('../../../utils/md5.js')


import moment from '../../../utils/moment'
import md5 from '../../../utils/md5'
import req from '../../../utils/req'
moment.locale('zh-cn', {
  weekdays: '周日_周一_周二_周三_周四_周五_周六'.split('_'),
})
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    clicked: true,
    sign: true,
    step: 0,
    userInfo: '',
    share: true,
    openid: '',
    isHaveopenid: true,
    page: 1,
    haveMore: true,
    noMore: '',
    newUser: true,
    newuserclick: true,
    isOpenWXRun: true,
    isOpenWXRuns: true, //为了区分运动授权，打开运动设置
    is_dh: '',
    goods_id: '',
    zl: '',
    jx: '',
    total_currency: '',
    today_currency: '',
    dhcg: false,
    bespoke: false,
    isHaveopenid: true,
    bj_picture: 'https://qizhifan.oss-cn-hangzhou.aliyuncs.com/zdd/system/applet/homeBg.png',
<<<<<<< HEAD
    notice_title: '',
    dayed: 1,
=======
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
    x_times: '',
    money: '',
    ad_data: {},
    plate: '',
    erweima: '',
    seat: '', //签到12，步数兑换11,幸运奖励13
    ra: '',
    flow: false, //防止流量主广告点击回来出弹框
    result: '',
    cj: '',
    cj_id: '',
    sharechou: '',
    signfail: true,
    signbiao: '',
    everything: {
      is_add: '',
      is_challenge: '',
      is_follow: '',
      is_lucky_draw: '',
      is_new: '',
      is_qun: '',
      is_red: '',
      is_rule: '',
      is_sign: '',
      is_turntable: '',
      share_act: '',
    },
    ad_id: '',
    is_chouswitch: true,
    isShowInterstitialAd: 1,
    step_adv_type: 1,
    wek: ['一', '二', '三', '四', '五', '六', '日'],
    // nowDate: 3,
    nowDate: new Date().getDay() !== 0 ? new Date().getDay() - 1 : 6,
    selectresult: {
      page: 1,
      size: 10,
    },
    listData: [],
    signData: {},
    showdayn: 0, //1展示签到成功,2展示连续签到弹窗 0:不显示
    showchange: 0, // 1显示兑换弹窗2显示兑换成功弹窗
    listvideoData: [], //直播弹窗
    orderSuccess: false,
    show: 0,
<<<<<<< HEAD
    isgetphone: false
=======
    step_currency: 0
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 没有登录
    if (!wx.getStorageSync('openid')) {
      wx.login({
        success: res => {
          wx.getUserInfo({
            withCredentials: true,
            success: res_user => {
              console.log(res_user)
              var object = {
                record_type: 1,
                auth_type: 0,
                scene_value: 0,
                code: res.code,
                encryptedData: encodeURIComponent(res_user.encryptedData),
                iv: encodeURIComponent(res_user.iv),
              }
              var sign = app.sort_ASCII(object)
              wx.request({
                url: app.globalData.new_url + '/user/login/login',
                data: {
                  record_type: 1,
                  auth_type: 0,
                  scene_value: 0,
                  code: res.code,
                  encryptedData: encodeURIComponent(res_user.encryptedData),
                  iv: encodeURIComponent(res_user.iv),
                  sign: sign,
                },
                method: 'POST',
                header: {
                  'content-type': 'application/json',
                },
                success: res => {
                  wx.setStorageSync('session', res.data.hash)
                  wx.setStorageSync('openid', res.data.openid)
                  wx.setStorageSync('open_id', res.data.open_id)
                  if (wx.getStorageSync('openid')) {
                    setTimeout(() => {
                      this.getVideolist()
                    }, 100)
                  }
                },
              })
            },
          })
        },
      })
    }
    let systemInfo = wx.getSystemInfoSync()
    // px转换到rpx的比例
    let pxToRpxScale = 750 / systemInfo.windowWidth
    // 状态栏的高度
    let ktxStatusHeight = systemInfo.statusBarHeight * pxToRpxScale
    // 导航栏的高度
    let navigationHeight = 44 * pxToRpxScale
    this.setData({
      ktxStatusHeight: ktxStatusHeight,
      navigationHeight: navigationHeight,
    })

    wx.showLoading({
      title: '加载中',
    })
<<<<<<< HEAD
    const that = this
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-deaf9f2dbf0fe8a3',
      })
      interstitialAd.onError(err => {
        console.log('onError event emit', err)
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
      })
    }
    if (wx.createRewardedVideoAd) {
      videoAd = wx.createRewardedVideoAd({
        adUnitId: 'adunit-d760aa330a1e1bcb',
      })
      videoAd.load()
      videoAd.onError(function (res) {
        console.log(101, res)
        if (
          res.errMsg == 'no advertisement' ||
          res.errMsg == 'no ad unit id' ||
          res.errCode == 1004
        ) {
          that.setData({
            noluck: true,
          })
        }
      })
      videoAd.onClose(status => {
        if (status && status.isEnded) {
          if (that.data.signbiao == 333) {
            var object = {
              unionid: wx.getStorageSync('openid'),
            }
            var sign = app.sort_ASCII(object)
            wx.request({
              url: app.globalData.new_url + '/userinfo/Userinfo/sign_ad',
              data: {
                unionid: wx.getStorageSync('openid'),
                sign: sign,
              },
              method: 'POST',
              success: function (res) {
                console.log('签到', res)
                that.music()
                if (res.data.code === 200) {
                  that.setData({
                    sign: false,
                    today_rlb: res.data.today_rlb,
                    tomorrow_rlb: res.data.receive_rlb,
                    dayed: res.data.count_day,
                  })
                }
                that.onShow()
              },
            })
          } else {
            that.music()
            var object = {
              unionid: wx.getStorageSync('openid'),
            }
            var sign = app.sort_ASCII(object)
            wx.request({
              url: app.globalData.new_url + '/userinfo/Userinfo/incentive',
              data: {
                unionid: wx.getStorageSync('openid'),
                sign: sign,
              },
              method: 'POST',
              success: function (res) {
                console.log(321, res)
                that.onShow()
              },
            })
          }
        } else {
          if (that.data.signbiao == 333) {
            that.setData({
              signfail: false,
            })
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
      })
    }
    if (options.jx == 55) {
      that.setData({
        jx: options.jx,
        openid: options.openid,
      })
    }
    if (options.xr == 77) {
      that.setData({
        openid: options.openid,
        goods_id: options.goods_id,
        xr: options.xr,
      })
    }
    if (options.zl == 66) {
      that.setData({
        zl: 66,
        goods_id: options.goods_id,
        openid: options.openid,
      })
      if (options.openid === wx.getStorageSync('openid')) {
        wx.navigateTo({
          url: '/pages/main/oneDetail/index?id=' +
            options.goods_id +
            '&&parements=ss',
        })
      } else if (wx.getStorageSync('openid')) {
        wx.navigateTo({
          url: '/pages/other/zhuli/index?goods_id=' +
            options.goods_id +
            '&&openid=' +
            options.openid,
        })
      } else {
        that.setData({
          newUser: false,
          newuserclick: true,
        })
      }
    }
    if (options.cj == 123) {
      that.data.cj = options.cj
      that.data.cj_id = options.id
      that.data.sharechou = options.share
      that.data.openid = options.openid
      if (options.openid === wx.getStorageSync('openid')) {
        wx.navigateTo({
          url: '/pages/other/choudetail/index?id=' + options.id,
        })
      } else if (wx.getStorageSync('openid')) {
        wx.navigateTo({
          url: '/pages/other/choudetail/index?id=' +
            options.id +
            '&&openid=' +
            options.openid +
            '&&share=' +
            options.share,
        })
      } else {
        that.setData({
          newUser: false,
          newuserclick: true,
        })
      }
    }
=======
    if (!wx.createRewardedVideoAd) {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
      })
    }
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
  },
  tointegral() {
    wx.navigateTo({
      url: '/pages/main/integral/index',
    })
  },
  
  sportSQS: function () {
    var that = this
    wx.getWeRunData({
      fail: function (res) {
        that.setData({
          isOpenWXRuns: false,
        })
      },
      complete: function () {
        that.onShow()
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (cb) {
    this.setData({
      isgetphone:wx.getStorageSync('phone')?true:false
    })
    var that = this
    if (wx.getStorageSync('openid') && wx.getStorageSync('open_id') != 0) {
      that.setData({
        isHaveopenid: true,
      })
      that.onRun()
      var openids = wx.getStorageSync('openid')
    } else {
      that.setData({
        isHaveopenid: false,
      })
    }
    var sobject = {
      unionid: openids,
    }
    var ssign = app.sort_ASCII(sobject)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/home_content',
      data: {
        unionid: openids,
        sign: ssign,
      },
      method: 'POST',
      header: {
        'Cache-Control': 'max-age=60,public', //60秒
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)
        that.setData({
          everything: res.data.con,
          total_currency: res?.data?.user?.total_currency,
          today_currency: res?.data?.user?.today_currency,
          result: res.data.weight_data,
          is_chouswitch: res.data.is_block,
          step_adv_type: res.data.step_adv_type,
        });
<<<<<<< HEAD
        (that.data.ad_id = res.data.step_ad),
        typeof cb == 'function' && cb(that.data.everything.is_new)
        if (
          res.data.scene_value == 1 &&
          that.data.jx != 55 &&
          that.data.xr != 77
        ) {
          if (res.data.weight_data.type_id == 2) {
            that.setData({
              zanwu: false,
            })
          } else {
            that.setData({
              measure: false,
            })
          }
        }
=======
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
      },
    })
    if (wx.getStorageSync('openid')) {
      this.getVideolist()
    }
  },
  onHide: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this
    wx.showNavigationBarLoading() //在标题栏中显示加载
    that.onShow() // 刷新页面
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  stepchange: util.throttle(function (e) {
    if (this.data.step < 2000) {
      return wx.showToast({
        title: '兑换的步数必须大于等于2000步',
        icon: 'none'
      })
    }
    const that = this
    var step = that.data.step
    var money = (step / 5000).toFixed(2)
    var object = {
      unionid: wx.getStorageSync('openid'),
      ad_id: 0,
      type: 1,
      step_num: step,
    }
    var sign = app.sort_ASCII(object)
    console.log(sign)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/exchange',
      data: {
        step_num: step,
        ad_id: 0,
        type: 1,
        unionid: wx.getStorageSync('openid'),
        sign: sign,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
      },
      success: res => {
        that.onShow()
        if (res.data.code == 200) {
          that.setData({
            stepdh: false,
            x_times: res.data.x_times,
            showchange: 2,
<<<<<<< HEAD
            step_currency: res.step_currency
=======
            step_currency: res.data.step_currency
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
          })
          if (res.data.ad) {
            that.setData({
              ad_data: res.data.ad,
              showchange: 2,
<<<<<<< HEAD
              step_currency: res.step_currency
=======
              step_currency: res.data.step_currency
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
            })
          }

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
    })
  }, 1500),
  exchange(e) {
    const that = this
    var is_dh = that.data.is_dh
    var step = that.data.step
    if (step < 2000) {
      return wx.showToast({
        title: '兑换的步数必须大于等于2000步',
        icon: 'none'
      })
    }
    var money = (step / 5000).toFixed(2)
    that.data.ra = money
    that.setData({
      dhcg: true,
    })
    if (step < 50) {
      wx.showToast({
        title: '抱歉，您的步数不足！兑换步数需大于等于50步',
        icon: 'none',
      })
    } else if (is_dh == 1) {
      this.setData({
        showchange: 1,
        money: money,
      })
    } else if (is_dh == 2) {
      wx.showToast({
        title: '抱歉，您今天兑换步数已达上限！每天最多兑换2万步',
      })
    } else {
      wx.showModal({
        content: '暂无获取到步数，请重新获取',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            that.setData({
              dhcg: false,
            })
            that.onShow()
          }
        },
      })
    }
  },
  onRun: function () {
    var that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.werun']) {
          that.setData({
            isOpenWXRun: false,
          })
        } else {
          if (!wx.getStorageSync('session')) {
            that.setData({
              isOpenWXRun: false,
            })
          } else {
            wx.checkSession({
              success: () => {
                wx.getWeRunData({
                  success: res => {
                    var object = {
                      encryptedData: encodeURIComponent(res.encryptedData),
                      iv: encodeURIComponent(res.iv),
                      session: wx.getStorageSync('session'),
                      unionid: wx.getStorageSync('openid'),
                    }
                    var sign = app.sort_ASCII(object)
                    wx.request({
                      url: app.globalData.new_url + '/user/login/wxrun',
                      data: {
                        encryptedData: encodeURIComponent(res.encryptedData),
                        iv: encodeURIComponent(res.iv),
                        session: wx.getStorageSync('session'),
                        unionid: wx.getStorageSync('openid'),
                        sign: sign,
                      },
                      method: 'POST',
                      header: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                      },
                      success: res => {
                        console.log('4', res)
                        if (res.data.status == 1) {
                          // req.getrewardStep().then(res2 => {
                          that.data.is_dh = res.data.is_dh
                          that.setData({
                            isOpenWXRun: true,
                          })

                          let total = res.data.data
            
                          const isAdd = total > that.data.step;
                          let timer = setInterval(() => {

                            if (typeof that.data.step === 'number' && isAdd && total - 5 > that.data.step) {
                              
                              that.setData({
                                step: that.data.step + 5,
                              })
                            } else if (typeof that.data.step === 'number' && !isAdd && that.data.step - 5 > total) {
                              
                              that.setData({
<<<<<<< HEAD
                                step: typeof that.data.step === 'number' ? total - 5 > that.data.step ? that.data.step + 5 : total :
                                  0,
=======
                                step: that.data.step - 5,
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
                              })
                            } else {

                              that.setData({
                                step: total
                              })
                              
                              timer && clearInterval(timer)
                            }
                          }, 1)
                          // })
                        } else {
                          that.setData({
                            isOpenWXRun: false,
                          })
                          wx.login({
                            success: res => {
                              wx.getUserInfo({
                                withCredentials: true,
                                success: function (res_user) {
                                  var object = {
                                    record_type: 1,
                                    auth_type: 0,
                                    scene_value: 0,
                                    code: res.code,
                                    encryptedData: encodeURIComponent(
                                      res_user.encryptedData
                                    ),
                                    iv: encodeURIComponent(res_user.iv),
                                  }
                                  var sign = app.sort_ASCII(object)
                                  wx.request({
                                    url: app.globalData.new_url +
                                      '/user/login/login',
                                    data: {
                                      record_type: 1,
                                      auth_type: 0,
                                      scene_value: 0,
                                      code: res.code,
                                      encryptedData: encodeURIComponent(
                                        res_user.encryptedData
                                      ),
                                      iv: encodeURIComponent(res_user.iv),
                                      sign: sign,
                                    },
                                    method: 'POST',
                                    header: {
                                      'content-type': 'application/json',
                                    },
                                    success: function (res) {
                                      wx.setStorageSync(
                                        'session',
                                        res.data.hash
                                      )
                                      wx.setStorageSync(
                                        'openid',
                                        res.data.openid
                                      )
                                      wx.setStorageSync(
                                        'open_id',
                                        res.data.open_id
                                      )
                                      that.onShow()
                                    },
                                  })
                                },
                              })
                            },
                          })
                        }
                      },
                    })
                  },
                })
              },
              fail: function () {
                wx.login({
                  success: res => {
                    if (res.code) {
                      wx.getUserInfo({
                        withCredentials: true,
                        success: function (res_user) {
                          var object = {
                            record_type: 1,
                            auth_type: 0,
                            scene_value: 0,
                            code: res.code,
                            encryptedData: encodeURIComponent(
                              res_user.encryptedData
                            ),
                            iv: encodeURIComponent(res_user.iv),
                          }
                          var sign = app.sort_ASCII(object)
                          wx.request({
                            url: app.globalData.new_url + '/user/login/login',
                            data: {
                              record_type: 1,
                              auth_type: 0,
                              scene_value: 0,
                              code: res.code,
                              encryptedData: encodeURIComponent(
                                res_user.encryptedData
                              ),
                              iv: encodeURIComponent(res_user.iv),
                              sign: sign,
                            },
                            method: 'POST',
                            header: {
                              'content-type': 'application/json',
                            },
                            success: function (res) {
                              wx.setStorageSync('session', res.data.hash)
                              wx.setStorageSync('openid', res.data.openid)
                              wx.setStorageSync('open_id', res.data.open_id)
                              that.onShow()
                            },
                          })
                        },
                      })
                    } else {
                      console.log('获取用户登录态失败！' + res.errMsg)
                    }
                  },
                })
              },
            })
          }
        }
      },
    })
  },
<<<<<<< HEAD
  navigateToAd: function (e) {
    const that = this
    that.data.seat = e.target.dataset.seat
    const plate = e.target.dataset.plate
    that.data.plate = plate
    const ad_data = that.data.ad_data
    const ad_id = ad_data.ad_id
    var x_times = that.data.x_times
    var ra = that.data.ra
    that.data.ra = ra
    if (ad_data.way == 1) {
      wx.navigateToMiniProgram({
        appId: ad_data.appid,
        path: ad_data.path,
        success(res) {
          var object = {
            unionid: wx.getStorageSync('openid'),
            type: 1,
            ad_id: ad_id,
            plate: plate,
            ra: ra,
            ratio: x_times,
          }
          var sign = app.sort_ASCII(object)
          wx.request({
            url: app.globalData.new_url + '/adauth/Adv/back_adv',
            data: {
              unionid: wx.getStorageSync('openid'),
              type: 1,
              ad_id: ad_id,
              plate: plate,
              ra: ra,
              ratio: x_times,
              sign: sign,
            },
            method: 'POST',
            header: {
              'content-type': 'application/json',
            },
            success: function (res) {
              console.log('跳转成功', res)
              that.setData({
                dhcg: false,
                sign: true,
                stepdh: true,
                luck: true,
              })
              that.data.flow = true
            },
          })
        },
      })
    } else {
      if (plate == 1) {
        var step = that.data.step
        wx.navigateTo({
          url: '../../other/erweima/index?ad_id=' +
            ad_id +
            '&&plate=' +
            plate +
            '&&ra=' +
            ra +
            '&&x_times=' +
            x_times +
            '&&step=' +
            step,
        })
      } else {
        wx.navigateTo({
          url: '../../other/erweima/index?ad_id=' +
            ad_id +
            '&&plate=' +
            plate +
            '&&rand_rlb=' +
            ra,
        })
      }
      that.setData({
        sign: true,
        stepdh: true,
        dhcg: false,
        luck: true,
      })
    }
  },
=======
  
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
  goSignForm: util.throttle(function (e) {
    //已经签到
    if (this.data.everything.step_sign) {
      wx.navigateTo({
        url: `/pages/main/signdetail/history/index?dayn=${this.data.signData.dayn}`,
      })
      return
    }
    const that = this
    // if (that.data.noluck) {
    var object = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/user/integral/step_sign',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign,
      },
      method: 'POST',
      success: res => {
        console.log('签到', res)
        if (res.data.code === 200) {
          this.setData({
            signData: res?.data.data,
            showdayn: 1,
          })
        }
        // that.onShow()
      },
    })

  }, 1500),


  rule: util.throttle(function (e) {
    const that = this
    var object = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/rule_explain',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign,
      },
      method: 'POST',
      success: function (res) {
        wx.navigateTo({
          url: '../../other/rule/index',
        })
      },
    })
  }, 1500),
  authorizeNows: function (e) {
    const that = this
    if (that.data.openid) {
      app.xrLogin(function (res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading()
          that.sportSQS()
          that.setData({
            shouquan: true,
            shouIndex: false,
          })
        }
      })
    } else {
      app.noautoLogin(function (res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading()
          that.sportSQS()
          that.setData({
            newUser: true,
            newuserclick: false,
          })
        }
      })
    }
  },
  authorizeNow: function (e) {
    const that = this
    if (that.data.openid) {
      app.xrLogin(function (res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading()
          that.onShow()
          that.setData({
            shouquan: true,
            shouIndex: false,
          })
        }
      })
    } else {
      app.noautoLogin(function (res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading()
          that.onShow()
          that.setData({
            newUser: true,
            newuserclick: false,
          })
        }
      })
    }
  },


  shouquan: function () {
    const that = this
    that.setData({
      shouquan: true,
      shouIndex: false,
    })
  },


  // 显示签到弹窗或者连续签到弹窗
  closeSign(e) {
    this.onRun()
    this.setData({
      showdayn: this.data.signData.dayn == 7 || this.data.signData.dayn == 15 ?
        e.currentTarget.id : 0,
    })
    this.onShow()

  },
  close(e) {
    this.setData({
      showchange: 0,
      orderSuccess: false
    })
    this.onRun()
  },
<<<<<<< HEAD
  newUserLingqus: function () {
    const that = this
    if (that.data.zl == 66) {
      var goods_id = that.data.goods_id
      var openid = that.data.openid
      app.xrLogin(function (res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading()
          wx.navigateTo({
            url: '/pages/other/zhuli/index?goods_id=' +
              goods_id +
              '&&openid=' +
              openid,
          })
          that.setData({
            newUser: true,
            newuserclick: false,
          })
        }
      })
    } else if (that.data.cj == 123) {
      var id = that.data.cj_id
      var openid = that.data.openid
      var share = that.data.sharechou
      app.xrLogin(function (res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading()
          wx.navigateTo({
            url: '/pages/other/choudetail/index?id=' +
              id +
              '&&openid=' +
              openid +
              '&&share=' +
              share,
          })
          that.setData({
            newUser: true,
            newuserclick: false,
          })
        }
      })
    } else if (that.data.openid) {
      app.xrLogin(function (res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading()
          that.onShow()
          that.setData({
            newUser: true,
            zllqcg: false,
            newuserclick: false,
          })
        }
      })
    } else {
      app.onLogin(function (res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading()
          that.onShow()
          that.setData({
            newUser: true,
            zllqcg: false,
            newuserclick: false,
          })
        }
      })
    }
  },
  golqnew: util.throttle(function (e) {
    const that = this
    that.data.form_id = e.detail.formId
    var object = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/new_welfare',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
      },
      success: function (res) {
        console.log(res)
        that.onShow()
        that.music()
        that.setData({
          zllqcg: false,
        })
      },
    })
  }, 1500),
  wsResult: util.throttle(function (e) {
    var that = this
    var userInfo = that.data.userInfo
    var gender = null
    if (e.detail.value.gender > 0) {
      gender = Number.parseInt(e.detail.value.gender) + 1
    } else if (userInfo.gender > 0) {
      gender = userInfo.gender
    } else {
      gender = ''
    }
    var height = null
    if (e.detail.value.height > 0) {
      height = e.detail.value.height
    } else if (userInfo.height > 0) {
      height = userInfo.height
    } else {
      height = ''
    }
    var weight = null
    if (e.detail.value.weight > 0) {
      weight = e.detail.value.weight
    } else if (userInfo.weight > 0) {
      weight = userInfo.weight
    } else {
      weight = ''
    }
    let age = e.detail.value.age
    let mes = ''
    if (weight === '' || weight == 0) {
      mes = '体重'
      wx.showModal({
        title: '信息不完整',
        content: `${mes}未填写，请补充`,
        showCancel: false,
        confirmText: '知道了',
        success: function (res) {},
      })
      return
    }
    if (height === '' || height == 0) {
      mes = '身高'
      wx.showModal({
        title: '信息不完整',
        content: `${mes}未填写，请补充`,
        showCancel: false,
        confirmText: '知道了',
        success: function (res) {},
      })
      return
    }
    if (gender === '' || gender == null) {
      mes = '性别'
      wx.showModal({
        title: '信息不完整',
        content: `${mes}未填写，请补充`,
        showCancel: false,
        confirmText: '知道了',
        success: function (res) {},
      })
      return
    }
    if (age === '' || age == 0) {
      mes = '年龄'
      wx.showModal({
        title: '信息不完整',
        content: `${mes}未填写，请补充`,
        showCancel: false,
        confirmText: '知道了',
        success: function (res) {},
      })
      return
    }
    var object = {
      weight: weight,
      gender: gender,
      age: age,
      height: height,
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/weighing/weighing/new_save_info',
      data: {
        weight: weight,
        gender: gender,
        age: age,
        height: height,
        unionid: wx.getStorageSync('openid'),
        sign: sign,
      },
      method: 'POST',
      success: function (res) {
        wx.navigateTo({
          url: '../../main/analysis/index',
        })
        that.setData({
          perMessage: true,
        })
      },
    })
  }, 1500),
  pickGender: function (e) {
    this.setData({
      [`userInfo.gender`]: Number.parseInt(e.detail.value) + 1,
    })
  },
  pickAge: function (e) {
    this.setData({
      [`userInfo.age`]: Number.parseInt(e.detail.value) + 1,
    })
  },
  pickHeight: function (e) {
    this.setData({
      [`userInfo.height`]: Number.parseInt(e.detail.value) + 120,
    })
  },
  pickWeight: function (e) {
    this.setData({
      [`userInfo.weight`]: Number.parseInt(e.detail.value) + 30,
    })
  },
  analysis: util.throttle(function () {
    const that = this
    var object = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    if (wx.getStorageSync('openid') && wx.getStorageSync('open_id') != 0) {
      wx.request({
        url: app.globalData.new_url + '/weighing/Weighing/see_test_info',
        data: {
          unionid: wx.getStorageSync('openid'),
          sign: sign,
        },
        method: 'POST',
        header: {
          'content-type': 'application/json',
        },
        success: function (res) {
          console.log(res)
          if (res.data.code === 200) {
            wx.navigateTo({
              url: '../../main/analysis/index',
            })
          } else {
            that.setData({
              userInfo: res.data.res,
              perMessage: false,
            })
          }
        },
      })
    } else {
      that.authorizeNow()
    }
  }, 1500),
  goHeat: util.throttle(function (e) {
    var that = this
    if (wx.getStorageSync('openid') && wx.getStorageSync('open_id') != 0) {
      wx.navigateTo({
        url: '../../main/heatMoney/index',
      })
    } else {
      that.authorizeNow()
    }
  }, 2000),
  opengchou: function () {
    wx.navigateTo({
      url: '/pages/other/choujiang/index?home=1',
    })
  },
  opengturntable: function () {
    wx.navigateTo({
      url: '/pages/main/turntable/index?home=1',
    })
  },
  opengchallenge: function () {
    wx.navigateTo({
      url: '/pages/other/challenge/index?home=1',
    })
  },
  opengred: function () {
    wx.navigateTo({
      url: '/pages/other/redbag/index?home=1',
    })
  },
  xrlqs: function () {
    const that = this
    that.setData({
        zanwu: true,
        measure: true,
        clicked: false,
        clim: false,
      },
      () => {
        if (interstitialAd) {
          interstitialAd.show().catch(err => {
            console.error(err)
          })
        }
      }
    )
  },
  sfxr: util.throttle(function () {
    const that = this
    that.setData({
        zllqcg: true,
        zlclickedcc: false,
      },
      () => {
        if (interstitialAd) {
          interstitialAd.show().catch(err => {
            console.error(err)
          })
        }
      }
    )
    that.onShow()
  }, 1500),

  xrlq: function () {
    const that = this
    that.setData({
      newUser: true,
      newuserclick: false,
    })
  },
  chengzhong: util.throttle(function (e) {
    const that = this
    var object = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/new_welfare',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
      },
      success: function (res) {
        that.setData({
          measure: true,
          zanwu: true,
          clicked: false,
          clim: false,
        })
        if (that.data.everything.is_new) {
          that.onShow(function (res) {
            that.setData({
              zllqcg: false,
            })
          })
        } else {
          wx.navigateTo({
            url: '/pages/main/xrGoods/index',
          })
        }
      },
    })
  }, 1500),
  gotoZhuanpan: util.throttle(function () {
    var that = this
    if (wx.getStorageSync('openid') && wx.getStorageSync('open_id') != 0) {
      wx.navigateTo({
        url: '../../main/turntable/index',
      })
    } else {
      that.authorizeNow()
    }
  }, 1500),
  zlDetail: util.throttle(function (e) {
    var form_id = e.detail.formId
    var id = e.detail.target.dataset.id
    wx.navigateTo({
      url: '../../main/oneDetail/index?id=' +
        id +
        '&&form_id=' +
        form_id +
        '&&parements=ss',
    })
  }, 1500),
  xrDetail: util.throttle(function (e) {
    var form_id = e.detail.formId
    var id = e.detail.target.dataset.id
    wx.navigateTo({
      url: '../../main/newDetail/index?id=' +
        id +
        '&&form_id=' +
        form_id +
        '&&parements=ss',
    })
  }, 1500),
  freeDetail: util.throttle(function (e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../../main/detail/index?id=' + id + '&&parements=ss',
    })
  }, 1500),
  seeMore: function () {
    wx.navigateTo({
      url: '../../main/moreGoods/index',
    })
  },
  seezlMore: function () {
    wx.navigateTo({
      url: '../../main/zlGoods/index',
    })
  },
  seexrMore: function () {
    wx.navigateTo({
      url: '../../main/xrGoods/index',
    })
  },
  gotoRule: function () {
    wx.navigateTo({
      url: '../../main/rule/index',
    })
  },

  shouquan: function () {
    const that = this
    that.setData({
      shouquan: true,
      shouIndex: false,
    })
  },
  random: function (e) {
    const that = this
    const plate = e.target.dataset.plate
    that.data.seat = e.target.dataset.seat
    that.data.plate = plate
    var ad_id = that.data.ad_id
    if (wx.getStorageSync('openid')) {
      var object = {
        unionid: wx.getStorageSync('openid'),
        ad_id: ad_id,
      }
      var sign = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/userinfo/Userinfo/home_lucky_ad',
        data: {
          unionid: wx.getStorageSync('openid'),
          ad_id: ad_id,
          sign: sign,
        },
        method: 'POST',
        header: {
          'content-type': 'application/json',
        },
        success: function (res) {
          console.log('幸运奖励', res)
          that.data.ra = res.data.rand_rlb
          if (res.data.ad.way == 1) {
            that.setData({
              luck: false,
              ad_data: res.data.ad,
            })
          } else {
            that.setData({
              ad_data: res.data.ad,
            })
            var ad_id = res.data.ad.ad_id
            var ra = res.data.rand_rlb
            wx.navigateTo({
              url: '../../other/erweima/index?ad_id=' +
                ad_id +
                '&&plate=' +
                plate +
                '&&rand_rlb=' +
                ra,
            })
          }
        },
      })
    } else {
      return
    }
  },

  openluck: util.throttle(function (e) {
    const that = this
    that.data.signbiao = 1
    videoAd.show().catch(err => {
      //失败重试
      videoAd.load().then(() => {
        videoAd.show()
      })
    })
    that.setData({
      stepdh: true,
      clickedsuc: false,
      trysuc: true,
      share: true,
    })
  }, 1500),

  opengz: function () {
    this.setData({
      gzjl: false,
    })
  },
  opengjinqun: function () {
    wx.navigateTo({
      url: '/pages/other/jinqun/index',
    })
  },
  openadd: function () {
    let that = this
    var object = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/is_add',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign,
      },
      method: 'POST',
      success: function (res) {
        that.setData({
          tjjl: false,
        })
      },
    })
  },
  continueplay: function () {
    const that = this
    if (that.data.seat == 12) {
      that.setData({
        sign: false,
        tryfail: true,
        clickedfail: false,
      })
    } else if (that.data.seat == 11) {
      that.setData({
        stepdh: false,
        tryfail: true,
        clickedfail: false,
      })
    } else if (that.data.seat == 13) {
      that.setData({
        luck: false,
        tryfail: true,
        clickedfail: false,
      })
    }
  },
  addclick: function () {
    const that = this
    that.setData({
      tjjl: true,
      addmy: false,
    })
  },
  gotochou: util.throttle(function () {
    var that = this
    if (wx.getStorageSync('openid') && wx.getStorageSync('open_id') != 0) {
      wx.navigateTo({
        url: '/pages/other/choujiang/index',
      })
    } else {
      that.authorizeNow()
    }
  }, 1500),

  gotoredbag: util.throttle(function () {
    var that = this
    if (wx.getStorageSync('openid') && wx.getStorageSync('open_id') != 0) {
      wx.navigateTo({
        url: '/pages/other/redbag/index',
      })
      that.setData({
        zllqcg: true,
        zlclickedcc: false,
      })
    } else {
      that.authorizeNow()
    }
  }, 1500),
  gzclick: function () {
    const that = this
    that.setData({
      gzjl: true,
      contact: false,
    })
  },
  hideHandlead: function () {
    const that = this
    that.setData({
      share: true,
    })
    if (interstitialAd) {
      interstitialAd.show().catch(err => {
        console.error(err)
      })
    }
  },
  // 显示签到弹窗或者连续签到弹窗
  closeSign(e) {
    this.setData({
      showdayn: this.data.signData.dayn == 7 || this.data.signData.dayn == 15 ?
        e.currentTarget.id :
        0,
    })
  },
  close() {
    this.setData({
      showchange: 0,
    })
  },
=======
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
  hideHandle: function () {
    const that = this
    that.setData({
      sign: true,
      addmy: true,
      contact: true,
      tjjl: true,
      gzjl: true,
      luck: true,
      tryfail: true,
      stepdh: true,
      dhcg: false,
      signfail: true,
      perMessage: true,
      signData: {},
      showchange: false,
      showCancel: false,
      orderSuccess: false,
      show: false,
    })
  },

  cancleBtn() {
    this.setData({
      showchange: 0,
    })
  },
  surechange(e) {
    let id = e.currentTarget.id
    if (!e.detail.cloudID && !id) {
      return wx.showToast({
        title: '不授权将无法兑换积分',
        icon: 'none',
      })
    }
    let phone = id ? {} : e.detail
    const that = this
    var step = that.data.step
    var money = (step / 5000).toFixed(2)
    that.data.ra = money

    if (that.data.ad_id) {
      var ad_id = that.data.ad_id
      var object = {
        unionid: wx.getStorageSync('openid'),
        ad_id: ad_id,
        type: 2,
        step_num: step,
      }
      var sign = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/userinfo/Userinfo/exchange',
        data: {
          ...object,
          ...phone,
          sign: sign,
        },
        method: 'POST',
        header: {
          'content-type': 'application/json',
        },
        success: res => {
          that.onShow()
          if (res.data.code == 200) {
            wx.setStorageSync('phone', 1)
            that.setData({
              stepdh: false,
              x_times: res.data.x_times,
              showchange: 2,
<<<<<<< HEAD
              step_currency: res.step_currency,
              isgetphone: true
=======
              step_currency: res.step_currency

>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
            })
            if (res.data.ad) {
              that.setData({
                ad_data: res.data.ad,
<<<<<<< HEAD
=======
                showchange: 2,
                step_currency: res.step_currency
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
              })
            }
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
            })
          }
        },
      })
    } else {
      var object = {
        unionid: wx.getStorageSync('openid'),
        ad_id: 0,
        type: 1,
        ...phone,
        step_num: step,
      }
      var sign = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/userinfo/Userinfo/exchange',
        data: {
          ...object,
          sign: sign,
        },
        method: 'POST',
        header: {
          'content-type': 'application/json',
        },
        success: function (res) {
          that.onShow()
          if (res.data.code == 200) {
            wx.setStorageSync('phone', 1)
            that.setData({
              stepdh: false,
              x_times: res.data.x_times,
              showchange: 2,
<<<<<<< HEAD
              step_currency: res.step_currency,
              isgetphone: true
=======
              step_currency: res.data.step_currency
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b

            })
            if (res.data.ad) {
              that.setData({
<<<<<<< HEAD
                ad_data: res.data.ad
=======
                ad_data: res.data.ad,
                showchange: 2,
                step_currency: res.data.step_currency
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
              })
            }

          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
            })
          }
        },
      })
    }
  },

  // 获取直播信息

  // 获取直播间直播
  getlive(item) {
<<<<<<< HEAD
    item.record = {}
    item.record.live_id = "notice/UzFfAgtgekIEAQAAAAAAlxsjdm7E1QAAAAstQy6ubaLX4KHWvLEZgBPG1zKW1_6IvxcvpLWERud2SHj9_VwwFZSU8wsFtRtlAV7S7fXbRwj5_CWGTU3XbxHEV8JXNyKdtt4keGPnfdsmDs8Aagy1n5sQQ_HR_1EgWe6UjxMdame9HX29sGepb-EtLzqbHhvNc0fqpoQLr0yUtDBL9H51qhFD-m57PwF6JCs0uyKakIteJSSJ-KY0wLzUlqYuRSpItxQw"

=======
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
    let id = item.video_id
    wx.getChannelsLiveNoticeInfo({
      finderUserName: id,
      success: res => {
        res.isorder = false
        res.start = moment(parseInt(res.startTime) * 1000).format(
          ' M月DD日(dddd)  HH:mm'
        )
        if (item.record && item.record.live_id) {
<<<<<<< HEAD
          res.isorder = res.noticeId == item.record.live_id
=======
          res.isorder =md5(res.noticeId) == item.record.live_id
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
        }
        res.isshow= moment(parseInt(res.startTime) * 1000).isBefore(moment()) ? false : true

        res.video_id = id
        res.reward = item.reward
        wx.getChannelsLiveInfo({
          finderUserName: id,
          success: res1 => {
            if (res1.description) {
              res.description = res1.description
              res.reward == 1 &&
                req
                .getSubscribeList({
<<<<<<< HEAD
                  live_id: item.video_id,
=======
                  live_id: md5(res.noticeId),
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
                })
                .then(res2 => {
                  res.invite = []
                  if (res2 && res2.length > 0) res.invite = res2
<<<<<<< HEAD
                  if (this.data.selectresult.page == 1) {
                    this.data.mylistData = []
                  }
=======
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
                  this.data.listvideoData.push(res)
                  this.setData({
                    listvideoData: this.data.listvideoData,
                  })
                })

              if (res.reward != 1) {
                this.data.listvideoData.push(res)
                this.setData({
                  listvideoData: this.data.listvideoData,
                })
              }
            }
          },
        })

        // wx.getChannelsLiveNoticeInfo()
      },
    })
  },
  inviteFriend(e) {
    console.log(this.data.listvideoData[e.currentTarget.id]);
    // let id = e.currentTarget.id
    //
    console.log(this.data.listvideoData[e.currentTarget.id])
    this.setData({
      show: 1,
      selectData: this.data.listvideoData[e.currentTarget.id],
    })
  },
  makeorder(e) {
    let id = this.data.listvideoData[e.currentTarget.id].noticeId
    wx.reserveChannelsLive({
      noticeId: id,
      success: res => {
        if (res.state == 6) {
          this.submitOrder(id)
        }
      },
      fail: err => {
        console.log(err)
      },
    })
    this.setData({
      selectData: this.data.listvideoData[e.currentTarget.id],
    })
  },

  getVideolist() {
    this.data.listvideoData = []
    req.getVideoList().then(res => {
      res &&
        res.map(item => {
          item.show == 1 && this.getlive(item)
        })
    })
  },
  submitOrder(id) {
    let {
      headUrl: video_headimg,
      nickname: video_name,
      startTime: live_time,
      description,
      video_id,
    } = this.data.selectData
    req
      .subscribeLive({
        live_id: this.data.selectData.noticeId,
        video_headimg,
        video_name,
        live_time,
        description,
        video_id,
        p_unionid: '',
      })
      .then(res => {
        console.log(res);
        if (res.code == 200) {
          res &&
            this.setData({
              orderSuccess: this.data.shareId ? false : true,
            })
          this.onRun()
          this.getlive()
          setTimeout(() => {
            this.setData({
              shareOrderSuccess: false,
            })
          }, 3000)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }

      })
  },
  toothermin() {
    wx.navigateToMiniProgram({
      appId: 'wx8e8e2602586085f3',
<<<<<<< HEAD
      path: 'packages/pointstore/point-center/index?kdt_id=18748287'
=======
      path: 'pages/common/blank-page/index?weappSharePath=pages%2Fhome%2Ffeature%2Findex%3Falias%3DU0bciXXVll%26kdt_id%3D18748287'
>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(res) {
    return {
      title: '',
      path: `/pages/main/live/live?shareId=${wx.getStorageSync(
        'openid'
      )}&video_id=${this.data.selectData.video_id}`,
    }
  },
<<<<<<< HEAD
  // onReachBottom() {
  //     if (this.listData.length < this.total) {
  //         let current = this.data.selectresult.page
  //         this.data.selectresult.page = current + 1
  //         this.getlist()
  //     }
  // },
=======

>>>>>>> 87046a6b4b79664257b84eb714a03871cfc3514b
})